package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.biz.TransOutboundInvoker;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.factory.OrderProcessPipelineFactory;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.service.OrderCoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class OrderCoreServiceImpl implements OrderCoreService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderProcessPipelineFactory orderProcessPipelineFactory;

    @Autowired
    StockMapper stockMapper;


    /**
     * 创建订单的处理流程
     *
     * @param request
     * @return
     */
    @Override
    public String createOrder(CreateOrderRequest request) {
        String orderId = null;
        try{
            //创建pipeline对象
            TransOutboundInvoker invoker = orderProcessPipelineFactory.build(request);

            //启动pipeline
            invoker.start(); //启动流程（pipeline来处理）

            //获取处理结果
            AbsTransHandlerContext context = invoker.getContext();

            //从Context中获取订单Id
            orderId = context.getOrderId();

        }catch (Exception e){
            log.error("OrderCoreServiceImpl.createOrder Exception :" + e);

        }

        return orderId;
    }

    /**
     * @Description: TODO 待付款订单取消
     * @param: orderId
     * @return:
     * @Author: mxy
     * @Date: 2022/12/3 10:24
    */
    @Override
    public void cancelOrder(String orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setStatus(7);
        order.setUpdateTime(new Date(System.currentTimeMillis()));
        orderMapper.updateByPrimaryKeySelective(order);
    }


    /**
     * @Description: TODO 删除订单
     * @param: [orderId]
     * @return: void
     * @Author: mxy
     * @Date: 2022/12/3 10:43
     */
    @Override
    public void deleteOrder(String orderId) {
        orderMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public void updateOrder(Integer status, String orderId) {


    }

}
